<?php
/**
 * Created at 2010-5-11 18:01:51
 * @author Jack
 * @code
	//示例
	$pager = new db_pager();
	$pager->set_db($db);
	$pager->set_cur_page('cur_page');
	$pager->set_per_page('per_page');
	$pager->set_sql($sql);
	$data_all = $pager->get_all();
	extract($data_all['params']);
	$page_control = get_ajax_page($total_count,'query',$per_page,$cur_page);
 * @endcode
 */

class db_pager{
    private $db = null;
	private $sql_data = null;
    private $sql = null;
    private $count_sql = null;
    private $per_page = 10;
    private $cur_page = 1;

    private $total_count = 0;
    private $total_page = 0;
    private $first_page = 1;
    private $last_page = 1;
    private $prev_page = 0;
    private $next_page = 0;
    private $data = null;
	private $enable_debug = false;

    public function __construct($p_db = null,$p_sql = null){
        $this->db = $p_db;
        $this->sql = $p_sql;
        $this->count_sql = $this->get_count_sql();
    }

	/**
	 * 获取COUNT语句
	 * */
    private function get_count_sql(){
        return preg_replace("/(select)(.+?)(from)/si", "\\1 count(*) \\3", $this->sql,1);
    }

	/**
	 * 判定SQL是否有效
	 * */
    private function is_valid_sql(){
        $match_count = preg_match("/(select)(.+?)(from)/si", $this->sql);
        return $match_count == 1;
    }

	/**
	 * 判定db类是否有效
	 * */
	private function is_valid_db(){
		if($this->db instanceof db_mysql){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 获取请求的变量，否则返回$default
	 * */
	private function get_request_value($key,$default = null){
		if(isset($_GET[$key])){
			return $_GET[$key];
		}elseif(isset($_POST[$key])){
			return $_POST[$key];
		}elseif(isset($_COOKIE[$key])){
			return $_COOKIE[$key];
		}else{
			return $default;
		}
	}


	/**
	 * 调试信息
	 * */
	private function debug($msg){
		if($this->enable_debug){
			die("<p>{$msg}<p>");
		}else{
			exit();
		}
	}

	
	/**
	 * 调试器开关
	 * */
	public function set_debug($flag = true){
		$this->enable_debug = $flag;
	}

	/**
	 * 设置当前页
	 * */
	public function set_cur_page($cur_page){
		if(is_string($cur_page)){
			$this->cur_page = $this->get_request_value($cur_page,1);
		}else{
			$this->cur_page = intval($cur_page);
		}
	}

	/**
	 * 设置每页数据量
	 * */
	public function set_per_page($per_page,$default = 5){
		if(is_string($per_page)){
			$this->per_page = $this->get_request_value($per_page,$default);
		}else{
			$this->per_page = intval($per_page);
		}
	}

	/**
	 * 设置SQL语句
	 * */
	public function set_sql($sql){
		$this->sql = $sql;
		$this->count_sql = $this->get_count_sql();
	}

	/**
	 * 设置db类
	 * */
	public function set_db($db){
		$this->db = $db;
	}

	/**
	 * 获取分页数据
	 * */
	public function get_all(){
        if(!$this->is_valid_sql()){
			$this->debug("<p>sql error : [".$this->sql."]</p>");
		}
		if(!$this->is_valid_db()){
			$this->debug("<p>db class error</p>");
		}
		$total_count = $this->total_count = $this->db->result_first($this->count_sql);
		$total_page = $this->total_page = intval(ceil($total_count / $this->per_page));
		$cur_page = $this->cur_page = $this->cur_page > $total_page?$total_page:$this->cur_page;
		$cur_page = $this->cur_page = $this->cur_page < 1?1:$this->cur_page;
		$last_page = $this->last_page = $total_page;
		$prev_page = $this->prev_page = $cur_page > 1?($cur_page-1):$cur_page;
		$next_page = $this->next_page = $cur_page < $total_page?($total_page-1):$total_page;
		$per_page = $this->per_page;
		$first_page = $this->first_page = 1;
		$start_limit = ($cur_page - 1) * $per_page;
		$this->sql_data = $this->sql . " LIMIT {$start_limit},{$per_page}";
		$data = $this->db->fetch_all($this->sql_data);
		$sql_data = $this->sql_data;
		$sql_count = $this->count_sql;

		$params = compact(
			'total_count','total_page',
			'cur_page','per_page',
			'first_page','last_page','prev_page','next_page',
			'sql_data','sql_count'
		);
		return compact('params','data');
	}
}
?>
